package chain;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author sk.z
 */
public class ChainImpl implements FilterChain {

    private Filter[] filterArr;

    private int pos = 0;
    private int addPos = 0;
    private static final int LENGTH = 10;

    @Override
    public void doFilter() {
        if (pos < addPos) {
            filterArr[pos++].doFilter(this);
        } else {
            pos = 0;
        }
    }

    public void addFilter(Filter filter) {
        if (Objects.isNull(this.filterArr)) {
            this.filterArr = new Filter[LENGTH];
        } else if ((addPos + 1) >= this.filterArr.length) {
            this.filterArr = Arrays.copyOf(this.filterArr, this.filterArr.length + LENGTH);
        }
        this.filterArr[addPos++] = filter;
    }

}
